//
//  NewsViewController.m
//  WangYi
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 0. All rights reserved.
//

#import "NewsViewController.h"
#import "DaoHangModel.h"

#import "NewsTableViewCell.h"
#import "NewsOneTableViewCell.h"
#import "NewsThreeTableViewCell.h"
#import "NewsLunboViewController.h"
@interface NewsViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIScrollView *titleScrollView;
@property(nonatomic,strong)UIScrollView *bigScrollView;
@property(nonatomic,strong)NSMutableArray *myArray;
@property(nonatomic,strong)NSMutableArray *vcArray;
@property(nonatomic,strong)UIView *tiaoView;
@property(nonatomic,strong)NSMutableArray *buttonArray;
@property(nonatomic,strong)NSArray *array;
@property(nonatomic,strong)NSMutableArray *allArray;
@property(nonatomic,strong)NSMutableArray *lbArray;
@property(nonatomic,strong)UIScrollView *myscrllView;

@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,copy)NSString *URL;
@end

@implementation NewsViewController



- (void)viewDidLoad {
    [super viewDidLoad];
      [self AFNetWorking];
    
    self.allArray = [NSMutableArray array];
    self.lbArray = [NSMutableArray array];
    self.buttonArray = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title =@"網易";
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH*100, HEIGHT*50)];
    label.font = [UIFont systemFontOfSize:HEIGHT*20];
    label.backgroundColor = [UIColor clearColor];
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text= self.navigationItem.title;
    self.navigationItem.titleView = label;
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    self.titleScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH*375-WIDTH*50, HEIGHT*40)];
    self.tiaoView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT*38, WIDTH*357/4, HEIGHT*2)];
    self.tiaoView.backgroundColor = [UIColor greenColor];
    [self.titleScrollView addSubview:self.tiaoView];
    self.titleScrollView.bounces = NO;
    self.titleScrollView.showsHorizontalScrollIndicator= NO;
    self.titleScrollView.contentSize = CGSizeMake(WIDTH*375/4*8, 0);
    [self.view addSubview:self.titleScrollView];
    self.titleScrollView.backgroundColor = [UIColor orangeColor];
    
    self.array = [NSArray arrayWithObjects:@"头条",@"娱乐",@"热点",@"体育",@"大连",@"图片",@"时尚",@"段子", nil];
    for (int i = 0 ; i<self.array.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:self.array[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake((WIDTH*375/4-5)*i, 0,WIDTH* 375/4,HEIGHT* 40);
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        button.tag = 1000 + i;
        [self.titleScrollView addSubview:button];
        [self.buttonArray  addObject:button];
    }
    self.bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, HEIGHT*40, WIDTH*375,HEIGHT* 667)];
    self.bigScrollView.contentSize = CGSizeMake(WIDTH*375*8, 0);
    self.bigScrollView.delegate = self;
    self.bigScrollView.pagingEnabled = YES;
    [self.view addSubview:self.bigScrollView];
    UIViewController *touVC = [[UIViewController alloc]init];
    touVC.view.frame = CGRectMake(0, 0, WIDTH*375,HEIGHT* 667);
    touVC.view.backgroundColor = [UIColor yellowColor];
    UIViewController *yuVC = [[UIViewController alloc]init];
    yuVC.view.frame = CGRectMake(WIDTH*375, 0, WIDTH*375, HEIGHT*667);
    UIViewController *reVC = [[UIViewController alloc]init];
    reVC.view.frame = CGRectMake(WIDTH*375*2, 0, WIDTH*375, HEIGHT*667);
    reVC.view.backgroundColor = [UIColor magentaColor];
    UIViewController *tiVC = [[UIViewController alloc]init];
    tiVC.view.frame = CGRectMake(WIDTH*375*3, 0, WIDTH*375,HEIGHT* 667);
    tiVC.view.backgroundColor = [UIColor greenColor];
    UIViewController *daVC = [[UIViewController alloc]init];
    daVC.view.frame = CGRectMake(WIDTH*375*4, 0, WIDTH*375, HEIGHT*667);
    UIViewController *tuVC = [[UIViewController alloc]init];
    tuVC.view.frame = CGRectMake(WIDTH*375*5, 0, WIDTH*375, HEIGHT*667);
    UIViewController *shiVC = [[UIViewController alloc]init];
    shiVC.view.frame = CGRectMake(WIDTH*375*6, 0, WIDTH*375, HEIGHT*667);
    UIViewController *duanVC = [[UIViewController alloc]init];
    duanVC.view.frame = CGRectMake(WIDTH*375*7, 0, WIDTH*375, HEIGHT*667);
    [self.vcArray addObject:touVC];
    [self.vcArray addObject:yuVC];
    [self.vcArray addObject:reVC];
    [self.vcArray addObject:tiVC];
    [self.vcArray addObject:daVC];
    [self.vcArray addObject:tuVC];
    [self.vcArray addObject:shiVC];
    [self.vcArray addObject:duanVC];
    [self addChildViewController:touVC];
    [self.bigScrollView addSubview:touVC.view];
    [self addChildViewController:yuVC];
    [self.bigScrollView addSubview:yuVC.view];
    [self addChildViewController:reVC];
    [self.bigScrollView addSubview:reVC.view];
    [self addChildViewController:tiVC];
    [self.bigScrollView addSubview:tiVC.view];
    [self addChildViewController:daVC];
    [self.bigScrollView addSubview:tuVC.view];
    [self addChildViewController:daVC];
    [self.bigScrollView addSubview:tuVC.view];
    [self addChildViewController:shiVC];
    [self.bigScrollView addSubview:shiVC.view];
    [self addChildViewController:duanVC];
    [self.bigScrollView addSubview:duanVC.view];
    self.myscrllView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH*375, HEIGHT*200)];
    self.myscrllView.contentSize = CGSizeMake(WIDTH*375*self.lbArray.count, 0);
    self.myscrllView.bounces = NO;
    self.myscrllView.pagingEnabled = YES;
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH*375, HEIGHT*667-HEIGHT*150) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.tableHeaderView = self.myscrllView;
    [self.bigScrollView addSubview:self.myTableView];
    
    
    
    // Do any additional setup after loading the view.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return HEIGHT*170;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DaoHangModel *model = [self.allArray objectAtIndex:indexPath.row];
    NSString *cellIdentify = @"cell";
  NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
   
    if (cell == nil) {
       
        cell = [[NewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    cell.model = model;
    if (!model.imgextra) {
        if (model.live_info != 0) {
            NSString *identify3 = @"cellw3";
            NewsOneTableViewCell *celle = [tableView dequeueReusableCellWithIdentifier:identify3];
            if (celle == nil) {
                celle = [[NewsOneTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify3];
            }
            celle.model = model;
            return celle;
        }
    }
    else if(![model.imgextra isEqual:nil]) {
        NSString *identify = @"cellw";
        NewsThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (cell == nil) {
            cell = [[NewsThreeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        }
        cell.model = model;
        return cell;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
//导航栏 点击事件
-(void)buttonAction:(UIButton *)button{
    for (int i =0; i<self.buttonArray.count; i++) {
        UIButton * button = [self.buttonArray objectAtIndex:i];
        [button setTitle:[self.array objectAtIndex:i ] forState:UIControlStateNormal];
   [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:HEIGHT*17];
    }
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.bigScrollView.contentOffset = CGPointMake(WIDTH*375 *(button.tag - 1000), 0);
    [UIView animateWithDuration:0.2 animations:^{
         self.tiaoView.frame = CGRectMake((WIDTH*375/4-WIDTH*5) * (button.tag - 1000), HEIGHT*38, WIDTH*375/4, HEIGHT*2);
        if ((button.tag-1000)>=0 &&(button.tag-1000)<= 2  ){
            self.titleScrollView.contentOffset = CGPointMake((WIDTH*375/4-5) *0, 0);
        }
        if (button.tag - 1000 >= 3 && button.tag - 1000 <= 5 ) {
            self.titleScrollView.contentOffset = CGPointMake((WIDTH*375/4-5)*(button.tag - 1000-1), 0);
        }
    }];
    }

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    for (int i = 0 ; i < self.buttonArray.count ; i++) {
     
        UIButton *select = self.buttonArray[i];
        [select setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    for (int i = 0; i <self.array.count; i ++) {
            if (self.bigScrollView.contentOffset.x  == self.view.frame.size.width *i) {
            UIButton *select = self.buttonArray[i];
            [select setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.tiaoView.frame = CGRectMake((WIDTH*375/4+WIDTH*30) * (scrollView.contentOffset.x/375*WIDTH), HEIGHT*38, WIDTH*375/4, HEIGHT*2);
    }];
   
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    for (int i = 0; i < self.buttonArray.count; i++) {
        UIButton *select = self.buttonArray[i];
        [select setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    
    for (int i = 0; i < self.array.count; i++) {
        
        if (self.bigScrollView.contentOffset.x == self.view.frame.size.width *i) {

        UIButton *select = self.buttonArray[i];
        [select setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
    }
}


//导航
-(void)AFNetWorking{
    
    NSString *url_string = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html"];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *Dic = responseObject;
        NSMutableArray *array =[Dic objectForKey:@"T1348647853363"];
        for (NSMutableDictionary *dic in array) {
            DaoHangModel *model = [[DaoHangModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:model];

            NSMutableArray *smallArray = [dic objectForKey:@"ads"];
            for (NSMutableDictionary *samllDic in smallArray) {
                         DaoHangModel *model = [[DaoHangModel alloc]init];
                        [model setValuesForKeysWithDictionary:samllDic];
                        [self.lbArray addObject:model];
  
            }
        }
        for (int i = 0; i < self.lbArray.count; i ++) {
            self.myscrllView.contentSize = CGSizeMake(WIDTH*375*self.lbArray.count, 0);
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH*375*i, 0, WIDTH*375, HEIGHT*200)];
            imageView.tag = 2000+i;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:tap];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[self.lbArray[i] imgsrc]] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
            [self.myscrllView addSubview:imageView];
        }
        
        [self.myTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
}
//轮播图  点击事件
-(void)tapAction:(UITapGestureRecognizer *)tap{
    NewsLunboViewController *lunBoVC = [[NewsLunboViewController alloc]init];
    DaoHangModel *model = [self.lbArray objectAtIndex:(tap.view.tag-2000)];
    //    截取字符串
    NSArray *array = [model.url componentsSeparatedByString:@"I"];
    // 拼接轮播图 详情网址
    NSString *a = [array objectAtIndex:1];
    NSArray *array2 = [a  componentsSeparatedByString:@"|"];
    NSString * bb = [array2[0] stringByAppendingString:@"/"];
    NSString * cc = [bb stringByAppendingString:array2[1]];
    self.URL = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/%@.json",cc];
    lunBoVC.URL = self.URL;
    lunBoVC.hidesBottomBarWhenPushed = YES;
    lunBoVC.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    [self.navigationController pushViewController:lunBoVC animated:YES];
    
    
    
    
    
}


-(void)viewWillDisappear:(BOOL)animated{
    
        self.navigationController.navigationBar.tintColor = [UIColor redColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
